<?php
if (isset($_SESSION['user_id']) && is_numeric($_SESSION['user_id'])) {
    require_once("{$CfgPara->root}/model/message.m.php");
    $myMessage = new message_m($CfgPara->database);
    if (isset($InputData['msg_edit_view'])) // 进入消息编辑器
    {
        //$InputData['msg_edit_view'] 
        // 进入消息编辑器 编辑类型 202意见反馈 203举报 204意见反馈对话 205私信对话 206系统消息(仅管理员)
        // 私信编辑视图
        if ($InputData['msg_edit_view'] == message_m::MsgPrivateDialogList) //205私信对话
        {
            $msg_reply_username = '';
            if (isset ($InputData['msg_dialog']) && $InputData['msg_dialog'] > 0) {
                // 获取对话对象的用户名
                $msg_reply_username = $myMessage->get_dialog_username($InputData['msg_dialog']);
            }
            $tittleText = '发送私信';
        } elseif ($InputData['msg_edit_view'] == message_m::MsgAdFeedback ||
            $InputData['msg_edit_view'] == message_m::MsgAdReport
        ) //反馈/举报
        {
            if ($InputData['msg_edit_view'] == message_m::MsgAdFeedback) {
                $tittleText = '意见反馈';
            } elseif ($InputData['msg_edit_view'] == message_m::MsgAdReport) {
                $tittleText = '举报内容';
                if (isset ($InputData['type']) && isset ($InputData['edit_pre'])) {
                    $edit_pre_text =
                        $myMessage->get_edit_pre($InputData['type'], $InputData['edit_pre']);
                }
            }
        } elseif ($InputData['msg_edit_view'] == message_m::MsgAdRepostDialogList) {
            $tittleText = '回复';
        } elseif ($InputData['msg_edit_view'] == message_m::MsgSystem) {
            $tittleText = '系统消息';
            $seleBarItem = 1;
            $seleBarItem1 = '发布';
            $seleBarItem2 = '历史';
            $seleBarScript1 = "goto_msg_edit(0,206,undefined,1)";
            $seleBarScript2 = "goto_dialog_show(0,206,1)";
        } else {
            echo '提交的信息错误';
            goto page_end;
        }
        if (!isset ($msg_reply_username)) {
            $msg_reply_username = '';
        }
        require("{$viewRoot}message_edit{$viewEx}");
    } elseif (isset ($InputData['msg_dialog_reply_edit_view']) &&
        isset ($InputData['parent_type']) &&
        isset ($InputData['parent_id']) &&
        isset ($InputData['msg_to_user_id'])
    ) {
        $tittleText = '回复';
        require("{$viewRoot}message_edit{$viewEx}");
    } elseif (isset ($InputData['msg_send'])) //消息发送操作
    {
        if ($InputData['msg_send'] == message_m::MsgPrivateDialogList &&
            isset($InputData['msg_to_username']) &&
            isset($InputData['msg_text']) &&
            isset ($InputData['img_names'])
        ) {
            //私信发送
            $result = $myMessage->msg_private_send(
                $InputData['msg_to_username'],
                $_SESSION['user_id'],
                $InputData['msg_text'],
                $InputData['img_names']);
            echo($result);
            goto page_end;
        } elseif ($InputData['msg_send'] == message_m::MsgAdFeedback ||
            $InputData['msg_send'] == message_m::MsgAdReport
        ) //意见反馈/举报操作
        {
            $result = $myMessage->msg_report(
                $InputData['msg_send'],
                $InputData['img_names'],
                $InputData['msg_text']);
            echo $result;
            goto page_end;
        } elseif ($InputData['msg_send'] == message_m::MsgAdRepostDialogList &&
            isset ($InputData['msg_dialog']) &&
            isset ($InputData['msg_text']) &&
            isset ($InputData['img_names'])
        ) //反馈/举报的对话回复
        {
            $result = $myMessage->msg_report_reply(
                $InputData['msg_dialog'],
                $InputData['msg_text'],
                $InputData['img_names']);
            echo $result;
            goto page_end;
        } elseif ($InputData['msg_send'] == message_m::MsgSystem &&
            isset ($InputData['msg_text']) &&
            isset ($InputData['img_names']) &&
            isset ($InputData['msg_tittle'])
        ) {
            $result = $myMessage->msg_system_send(
                $InputData['msg_tittle'],
                $InputData['msg_text'],
                $InputData['img_names']);
            echo $result;
            goto page_end;
        } else {
            echo '信息提交的数据错误';
        }
    } elseif (!isset($InputData['msg_type'])) {
        // 如果只是获取是否有未读消息
        if (isset ($InputData['get_has_new'])) {
            // 如果为管理员登陆
            if (isset ($_SESSION['user_level']) && $_SESSION['user_level'] >= 90 &&
                isset ($_SESSION['user_admin']) && $_SESSION['user_admin'] == 1
            ) {
                // 如果有未读的反馈或举报，返回1，否则返回-1
                $result = $myMessage->has_new_admin_msg();
                echo $result;
                goto page_end;
            }
        }

        // 进入普通用户消息查看页面 或 普通用户判断是否有新的未读消息
        $msgCount = $myMessage->msg_noread_count($_SESSION['user_id']);
        if (is_string($msgCount)) {
            echo($msgCount);
            goto page_end;
        }//如果为错误信息，直接返回错误文本

        // 判断当前用户有未查看的系统消息 如果有，则私信消息的未读数加1
        // LastMsgDt为最新系统消息的时间 ViewDt为用户最近查看系统消息的时间
        if (strtotime($msgCount->LastMsgDt) > strtotime($msgCount->ViewDt)) {
            $msgCount->Dialog = (int)$msgCount->Dialog + 1;
        }

        if (isset ($InputData['get_has_new'])) { //仅判断是否有新消息
            // 
            $allNoReadCount = (int)$msgCount->Remind + (int)$msgCount->Comment +
                (int)$msgCount->Follow + (int)$msgCount->Dialog;
            if ($allNoReadCount > 0) {
                echo 2;
            } else {
                echo -2;
            }
            goto page_end;
        } else  //加载消息首页
        {
            if ($isTablet === FALSE) {
                $tittleText = '我的消息';
            } else {
                $tittleText = '当前位置：SNEAKER CUSTOM / 我的消息';
            }
            require("{$viewRoot}tittle_back_bar{$viewEx}");
            require("{$viewRoot}message{$viewEx}");
        }
    } else {
        // 如果消息类型为管理员的反馈与举报消息 则需要检测当前用户是否为管理员登陆
        if ($InputData['msg_type'] == message_m::MsgAdDialog ||
            $InputData['msg_type'] == message_m::MsgAdFeedback ||
            $InputData['msg_type'] == message_m::MsgAdReport
        ) {
            if (isset ($_SESSION['user_admin']) && $_SESSION['user_admin'] == 1 &&
                isset ($_SESSION['user_level']) && $_SESSION['user_level'] > 90
            ) {
                $adminMsg = true;
                if (!isset ($InputData['page_change']))
                    require("{$viewRoot}menu_t_admin_msg{$viewEx}");
            } else {
                echo '无管理员权限，请以管理员登陆';
                goto page_end;
            }
        }
        $msgViewRoot = "{$CfgPara->root}/view/"; // 20170523 当前都使用手机端的页面
        if (!isset($InputData['page_change'])) {
            $page_index = 1;
            if ($isTablet === false) // 20170523 当前都使用手机端的页面
                $page_ex = '.app.v.php'; //加载整个视图
            else
                $page_ex = '.app.v.php';
            //清除掉当前消息类型的未读消息计数 
            //20170224 仅用户的消息需要清除, 管理员的消息需要打开查看后进行自减操作
            if (!isset ($adminMsg)) {
                $myMessage->msg_count_clear($InputData['msg_type'], $_SESSION['user_id']);
            }
        } else {
            $page_index = $InputData['page_change'];
            if ($isTablet === false)  // 20170523 当前都使用手机端的页面
                $page_ex = '_page.app.v.php'; //仅加载分页内容
            else {
                $page_ex = '_page.app.v.php';
            }
        }
        if ($InputData['msg_type'] == message_m::MsgRemind) {
            $msgPage = "{$msgViewRoot}message_remind{$page_ex}";
            if ($isTablet === FALSE) {
                $tittleText = '提醒';
            } else {
                $tittleText = '当前位置：SNEAKER CUSTOM / 我的消息 / 提醒';
            }
        } elseif ($InputData['msg_type'] == message_m::MsgComment) {
            $msgPage = "{$msgViewRoot}message_comment{$page_ex}";
            if ($isTablet === FALSE) {
                $tittleText = '评论';
            } else {
                $tittleText = '当前位置：SNEAKER CUSTOM / 我的消息 / 评论';
            }
        } elseif ($InputData['msg_type'] == message_m::MsgFollow) {
            $msgPage = "{$msgViewRoot}message_follow{$page_ex}";
            if ($isTablet === FALSE) {
                $tittleText = '关注';
            } else {
                $tittleText = '当前位置：SNEAKER CUSTOM / 我的消息 / 关注';
            }
        } elseif ($InputData['msg_type'] == message_m::MsgPrivate) {
            $msgPage = "{$msgViewRoot}message_private{$page_ex}";
            if ($isTablet === FALSE) {
                $tittleText = '私信';
            } else {
                $tittleText = '当前位置：SNEAKER CUSTOM / 我的消息 / 私信';
            }
        } elseif ($InputData['msg_type'] == message_m::MsgAdDialog) {
            $msgPage = "{$msgViewRoot}view/message_admin_dialog{$page_ex}";
            if ($isTablet === FALSE) {
                $tittleText = '消息';
            }
        } elseif ($InputData['msg_type'] == message_m::MsgAdFeedback ||
            $InputData['msg_type'] == message_m::MsgAdReport
        ) {
            $msgPage = "{$msgViewRoot}message_admin_report{$page_ex}";
            if ($isTablet === FALSE) {
                $tittleText = '消息';
            }
        } elseif ($InputData['msg_type'] == message_m::MsgAdRepostDialogList ||
            $InputData['msg_type'] == message_m::MsgPrivateDialogList
        ) {
            // 20170523 对话的内容列表 web与app版分离
            if (!isset($InputData['page_change'])) {
                if ($isTablet === false)
                    $page_ex = '.app.v.php'; //加载整个视图
                else
                    $page_ex = '.web.v.php';
            } else {
                if ($isTablet === false)
                    $page_ex = '_page.app.v.php'; //仅加载分页内容
                else
                    $page_ex = '_page.web.v.php';
            }
            // 20170302 添加 如果获取的是对话列表  对话表的标题在视图中设置
            if (isset ($InputData['msg_dialog'])) {
                if ($InputData['msg_type'] == message_m::MsgAdRepostDialogList) {
                    // 首先找到反馈/举报内容 即对话的第一个内容 后面再获取$msgList
                    if (!isset ($InputData['page_change'])) {
                        $msgReport = $myMessage->get_msg_report($InputData['msg_dialog']);
                        if (is_string($msgReport)) {
                            echo $msgReport;
                            goto page_end;
                        }
                    }

                    $msgPage = "{$viewRoot}message_admin_report_show{$page_ex}";
                } else
                    $msgPage = "{$viewRoot}message_private_show{$page_ex}";
            }
        } elseif ($InputData['msg_type'] == message_m::MsgSystem) {
            $msgPage = "{$msgViewRoot}message_system{$page_ex}";
            $tittleText = '系统消息';
        } elseif ($InputData['msg_type'] == message_m::MsgSystemContent) {
            //特殊处理 此处仅只有一个内容
            $msgDlg = $myMessage->get_msg_system($InputData['msg_dialog']);
            if (is_string($msgDlg)) {
                echo $msgDlg;
                goto page_end;
            }
            $tittleText = '系统消息';
            require("{$viewRoot}tittle_back_bar{$viewEx}");
            require("{$viewRoot}message_system_show{$page_ex}");
            goto page_end;
        }
        if ($InputData['msg_type'] == message_m::MsgAdRepostDialogList ||
            $InputData['msg_type'] == message_m::MsgPrivateDialogList
        )
            $msgCount = $myMessage->msg_count($InputData['msg_type'], $InputData['msg_dialog']);
        else
            $msgCount = $myMessage->msg_count($InputData['msg_type'], $_SESSION['user_id']);
        if (!is_numeric($msgCount)) {
            echo($msgCount);
            goto page_end;
        } //如果为错误信息，直接返回错误文本
        if ($msgCount > 0) {
            //连接分页类 由视图程序判断是否 在列表最后加入分页栏
            require_once("{$CfgPara->root}/class/paginate.class.php");
            $myPaginate = new paginate_c();
            $myPaginate->change_func = "message_page_change({$InputData['msg_type']},[x])";
            $paginateMsg = $myPaginate->paginate($page_index, ceil($msgCount / $CfgPara->pagesize));
            if ($InputData['msg_type'] == message_m::MsgAdRepostDialogList ||
                $InputData['msg_type'] == message_m::MsgPrivateDialogList
            ) {
                $msgList = $myMessage->msg_list($InputData['msg_type'], $InputData['msg_dialog'], $page_index, $CfgPara->pagesize);
            } else
                $msgList = $myMessage->msg_list($InputData['msg_type'], $_SESSION['user_id'], $page_index, $CfgPara->pagesize);
            if (is_string($msgList)) {
                echo($msgList);
                goto page_end;
            }
        }

        if (!isset($InputData['page_change'])) {
            // 20170224 修改 如果是管理员的反馈/举报消息 则调用的标题栏不同
            if ($InputData['msg_type'] == message_m::MsgAdRepostDialogList ||
                $InputData['msg_type'] == message_m::MsgPrivateDialogList
            ) {
                //视图中再动态调用 并生成标题
                //require("{$viewRoot}tittle_back_bar{$viewEx}");
            } elseif (!isset ($adminMsg)) {
                require("{$viewRoot}tittle_back_bar{$viewEx}");
            } else {
                require("{$viewRoot}tittle_back_bar{$viewEx}");
                $seleBarScript1 = "list_sele_bar_change(1);message_page_change(201,1)";
                $seleBarScript2 = "list_sele_bar_change(2);message_page_change(202,1)";
                $seleBarScript3 = "list_sele_bar_change(3);message_page_change(203,1)";
                if (isset ($InputData['msg_type'])) {
                    $seleBarItem = (int)$InputData['msg_type'] - 200;
                } else {
                    $seleBarItem = 1;
                }
                $seleBarItem1 = '对话';
                $seleBarItem2 = '反馈';
                $seleBarItem3 = '举报';
                $seleBarNew1 = 0;
                $seleBarNew2 = 0;
                $seleBarNew3 = 0;
                // 获取是否有新的未读反馈、举报
                $has_new = $myMessage->has_new_report();
                if (is_string($has_new)) {
                    echo $has_new;
                    goto page_end;
                }
                $seleBarNew2 = $has_new['Feedback'];
                $seleBarNew3 = $has_new['Report'];
                require("{$viewRoot}list_sele_bar_new{$viewEx}");
            }
        }
        require($msgPage);
    }
} else {
    if (isset($InputData['get_has_new'])) //如果只是读取消息数 不返回画面
    {
        echo -3;
    } else {
        require("{$CfgPara->root}/controller/user_login.c.php");
    }
}

page_end:;